package node_pieces

import (
	"database/sql/driver"
	"fmt"
)

// Value implements sql/driver.Valuer interface.
func (np NodePieces) Value() (driver.Value, error) {
	return np.Bytes(), nil
}

// Scan implements sql.Scanner interface.
func (np *NodePieces) Scan(value interface{}) error {
	var err error
	switch value := value.(type) {
	case []byte:
		*np, err = FromBytes(value)
		if err != nil {
			return err
		}
		return nil
	default:
		return fmt.Errorf("unable to scan %T into NodePieces", value)
	}
}

//
//func (NullNodePieces) GormDataType() string {
//	return ""
//}

type NullNodePieces struct {
	NodePieces NodePieces
	Valid      bool // Valid is true if UUID is not NULL
}

// Value implements sql/driver.Valuer interface.
func (n NullNodePieces) Value() (driver.Value, error) {
	if !n.Valid {
		return nil, nil
	}
	return n.NodePieces.Value()
}

// Scan implements sql.Scanner interface.
func (n *NullNodePieces) Scan(value interface{}) error {
	if value == nil {
		n.NodePieces, n.Valid = NodePieces{}, false
		return nil
	}

	n.Valid = true
	return n.NodePieces.Scan(value)
}
